/* linkit.js
 * By François Leporcq
 * MIT Licensed.
 */
var Rectangle = AbstractShape.extend({
    _className: "Rectangle",
    init: function(origin, width, height){
        this._origin = origin != null ? origin : new Point();
        this._width = width ? width : 0;
        this._height = height ? height : 0;
    },
    setOrigin: function(origin){
        this._origin = origin;
    },
    getOrigin: function(){
        return this._origin;
    },
    setWidth: function(width){
        this._width = width;
    },
    getWidth: function(){
        return this._width;
    },
    setHeight: function(height){
        this._height = height;
    },
    getHeight: function(){
        return this._height;
    },
    getCenter: function(){
      return   new Point(this.getOrigin().getX() + (this.getWidth() / 2), this.getOrigin().getY() + (this.getHeight() / 2));
    },
    getAnchor: function(point){
        var
        southWest = this.getOrigin(),
        southEast = new Point(this.getOrigin().getX() + this.getWidth() , this.getOrigin().getY()),
        northWest = new Point(this.getOrigin().getX(), this.getOrigin().getY() + this.getHeight()),
        northEast = new Point(this.getOrigin().getX() + this.getWidth(), this.getOrigin().getY() + this.getHeight());
        var cardinalLines = {
            "north" : new Line(northWest,northEast),
            "south" : new Line(southWest,southEast),
            "east" : new Line(northEast,southEast),
            "west" : new Line(northWest,southWest)
        };
        var line = new Line(this.getCenter(), point);

        for(var cardinalPoint in cardinalLines){
            var cardinalLine = cardinalLines[cardinalPoint];
            var intersection = cardinalLine.getIntersection(line);
            if (intersection !== null){
                return new Anchor(intersection.getX(), intersection.getY(), ECardinalPoint.get(cardinalPoint));
            }
        }

        return null;
    },
    getBox: function(){
        return this;
    }
}).implement([Linkable]);